package com.ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.dao.IStudentDao;
import com.ssm.entity.Course;
import com.ssm.entity.StudentCategory;

@Service
public class StudentServiceImpl {
    @Autowired
    private IStudentDao studentDao;
    
    // 查询所有学生分类
    public String findAllCategories() {
        List<StudentCategory> categories = studentDao.findAllCategories();
        JSONObject obj = new JSONObject();
        JSONArray data = new JSONArray();
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
        for (StudentCategory category : categories) {
            JSONArray arr = new JSONArray();
            arr.add(category.getCid());
            arr.add(category.getCname());
            arr.add(category.getDescription());
            
            String createTime = "";
            if (category.getCreate_time() != null) {
                if (category.getCreate_time() instanceof Date) {
                    createTime = sdf.format(category.getCreate_time());
                } else {
                    createTime = category.getCreate_time().toString();
                }
            } else {
                createTime = sdf.format(new Date());
            }
            arr.add(createTime);
            
            data.add(arr);
        }
        
        obj.put("data", data);
        return obj.toJSONString();
    }
    
    // 查询所有课程
    public String findAllCourses() {
        List<Map<String, Object>> courses = studentDao.findAllCourses();
        JSONObject obj = new JSONObject();
        JSONArray data = new JSONArray();
        
        for (Map<String, Object> course : courses) {
            JSONArray arr = new JSONArray();
            arr.add(course.get("course_id"));
            arr.add(course.get("course_name"));
            arr.add(course.get("credit"));
            arr.add(course.get("teacher_name"));
            arr.add(course.get("cname"));
            arr.add(course.get("description"));
            
            data.add(arr);
        }
        
        obj.put("data", data);
        return obj.toJSONString();
    }
    
    // 获取学生分类详情
    public String getCategory(int cid) {
        StudentCategory category = studentDao.getCategoryById(cid);
        if (category == null) {
            return "{}";
        }
        
        JSONObject obj = new JSONObject();
        obj.put("cid", category.getCid());
        obj.put("cname", category.getCname());
        obj.put("description", category.getDescription());
        
        return obj.toJSONString();
    }
    
    // 获取课程详情
    public String getCourse(int courseId) {
        Course course = studentDao.getCourseById(courseId);
        if (course == null) {
            return "{}";
        }
        
        JSONObject obj = new JSONObject();
        obj.put("course_id", course.getCourse_id());
        obj.put("course_name", course.getCourse_name());
        obj.put("credit", course.getCredit());
        obj.put("teacher_name", course.getTeacher_name());
        obj.put("description", course.getDescription());
        obj.put("cid", course.getCid());
        
        return obj.toJSONString();
    }
    
    // 添加学生分类
    public String addCategory(String cname, String description) {
        if (cname == null || cname.trim().isEmpty()) {
            return "{\"Status\":\"error\",\"msg\":\"分类名称不能为空\"}";
        }
        
        StudentCategory category = new StudentCategory();
        category.setCname(cname);
        category.setDescription(description);
        
        int count = studentDao.addCategory(category);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "分类添加成功" : "分类添加失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 更新学生分类
    public String updateCategory(int cid, String cname, String description) {
        if (cname == null || cname.trim().isEmpty()) {
            return "{\"Status\":\"error\",\"msg\":\"分类名称不能为空\"}";
        }
        
        if (cid <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"无效的分类ID\"}";
        }
        
        StudentCategory category = new StudentCategory();
        category.setCid(cid);
        category.setCname(cname);
        category.setDescription(description);
        
        int count = studentDao.updateCategory(category);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "分类更新成功" : "分类更新失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 删除学生分类
    public String deleteCategory(int cid) {
        if (cid <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"无效的分类ID\"}";
        }
        
        // 检查分类下是否有课程，如果有，则不允许删除
        int courseCount = studentDao.countCourseByCategory(cid);
        if (courseCount > 0) {
            return "{\"Status\":\"error\",\"msg\":\"该分类下有课程，不能删除\"}";
        }
        
        int count = studentDao.deleteCategory(cid);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "分类删除成功" : "分类删除失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 添加课程
    public String addCourse(String course_name, double credit, String teacher_name, String description, int cid) {
        if (course_name == null || course_name.trim().isEmpty()) {
            return "{\"Status\":\"error\",\"msg\":\"课程名称不能为空\"}";
        }
        
        if (credit <= 0 || credit > 10) {
            return "{\"Status\":\"error\",\"msg\":\"学分必须在0-10之间\"}";
        }
        
        if (cid <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"请选择有效的学生类别\"}";
        }
        
        Course course = new Course();
        course.setCourse_name(course_name);
        course.setCredit(new BigDecimal(credit));
        course.setTeacher_name(teacher_name);
        course.setDescription(description);
        course.setCid(cid);
        
        int count = studentDao.addCourse(course);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "课程添加成功" : "课程添加失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 更新课程
    public String updateCourse(int course_id, String course_name, double credit, String teacher_name, String description, int cid) {
        if (course_id <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"无效的课程ID\"}";
        }
        
        if (course_name == null || course_name.trim().isEmpty()) {
            return "{\"Status\":\"error\",\"msg\":\"课程名称不能为空\"}";
        }
        
        if (credit <= 0 || credit > 10) {
            return "{\"Status\":\"error\",\"msg\":\"学分必须在0-10之间\"}";
        }
        
        if (cid <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"请选择有效的学生类别\"}";
        }
        
        Course course = new Course();
        course.setCourse_id(course_id);
        course.setCourse_name(course_name);
        course.setCredit(new BigDecimal(credit));
        course.setTeacher_name(teacher_name);
        course.setDescription(description);
        course.setCid(cid);
        
        int count = studentDao.updateCourse(course);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "课程更新成功" : "课程更新失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 删除课程
    public String deleteCourse(int course_id) {
        if (course_id <= 0) {
            return "{\"Status\":\"error\",\"msg\":\"无效的课程ID\"}";
        }
        
        int count = studentDao.deleteCourse(course_id);
        String status = count > 0 ? "ok" : "error";
        String msg = count > 0 ? "课程删除成功" : "课程删除失败";
        
        JSONObject obj = new JSONObject();
        obj.put("Status", status);
        obj.put("msg", msg);
        return obj.toJSONString();
    }
    
    // 根据学生分类查询课程
    public String findCoursesByCategory(int cid) {
        List<Map<String, Object>> courses = studentDao.findCoursesByCategory(cid);
        JSONObject obj = new JSONObject();
        JSONArray data = new JSONArray();
        
        for (Map<String, Object> course : courses) {
            JSONArray arr = new JSONArray();
            arr.add(course.get("course_id"));
            arr.add(course.get("course_name"));
            arr.add(course.get("credit"));
            arr.add(course.get("teacher_name"));
            arr.add(course.get("cname"));
            arr.add(course.get("description"));
            
            data.add(arr);
        }
        
        obj.put("data", data);
        return obj.toJSONString();
    }
} 